package com.apes.autoparts.searcher.vin;

import com.apes.autoparts.searcher.Searcher;
import com.apes.framework.api.DomainService;
import com.apes.framework.api.SpringManager;
import com.apes.framework.plugin.cache.MemoryCache;
import com.apes.framework.plugin.esb.api.SimpleRequest;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 功能：Vin服务
 *
 * @author xul
 * @create 2019-08-14 11:06
 */
@Service("vinService")
public class VinService extends DomainService {

    /**
     * VIN解析
     **/
    public Map parseVin(SimpleRequest request) {
        return SpringManager.getBean(VinParser.class).parse(request.getJO());
    }

    /***
     * 关键词解析
     */

    public Map searchKeyword(SimpleRequest request) {
        return SpringManager.getBean(KeywordSearcher.class).searchKeyword(request.getJO());
    }

    /***
     * 目录名称解析
     * @param request
     * @return
     */
    public Map searchMenuName(SimpleRequest request) {
        return SpringManager.getBean(KeywordSearcher.class).searchMenuName(request.getJO());
    }


    /**
     * 获取目录
     **/
    public Map searchCatalog(SimpleRequest simpleRequest) {
        String dataSource = simpleRequest.get("dataSource");
        Searcher searcher = getSearcher(CatalogSearcher.class, dataSource);
        if(searcher == null) throw new RuntimeException("没有定义：" + dataSource + " 获取目录接口！");
        return (Map) searcher.parse(simpleRequest.getJO());
    }

    /**
     * 获取子组
     **/
    public Map searchSubgroup(SimpleRequest simpleRequest){
        String dataSource = simpleRequest.get("dataSource");
        Searcher searcher = getSearcher(SubgroupSearcher.class, dataSource);
        if(searcher == null) throw new RuntimeException("没有定义：" + dataSource + " 获取子组接口！");
        return (Map) searcher.parse(simpleRequest.getJO());
    }

    /**
     * 获取配件清单
     **/
    public Map searchPartsList(SimpleRequest simpleRequest){
        String dataSource = simpleRequest.get("dataSource");
        Searcher searcher = getSearcher(PartsListSearcher.class, dataSource);
        if(searcher == null) throw new RuntimeException("没有定义：" + dataSource + " 获取配件清单接口！");
        return (Map) searcher.parse(simpleRequest.getJO());
    }

    /***
     * TODO: 查询热点
     */

    /**
     * 通过OE获取商品
     **/
    public Map searchProducts(SimpleRequest simpleRequest){
        String dataSource = simpleRequest.get("dataSource");
        Searcher searcher = getSearcher(ProductsSearcher.class, dataSource);
        if(searcher == null) throw new RuntimeException("没有定义：" + dataSource + " 获取商品接口！");
        return (Map) searcher.parse(simpleRequest.getMap());
    }


    public static <T extends Searcher> T getSearcher(Class<T> clazz, String dataSource) {
        String key = "auto.vin." + clazz.getSimpleName();
        Map cache = (Map) MemoryCache.getInstance().get(key);
        if (cache == null) {
            synchronized (VinService.class) {
                cache = new HashMap<>();
                for (T executor : SpringManager.getBeansOfType(clazz).values()) {
                    for(String source: executor.getSource()) {
                        cache.put(source, executor);
                    }
                }
                MemoryCache.getInstance().put(key, cache);
            }
        }
        return (T) cache.get(dataSource);
    }
}
